from typing import Optional, List, Union, Any
from pydantic import BaseModel, Field

from app.schemas.base import BaseEntity, PageQuery


class MenuQuery(PageQuery):
    """菜单查询参数"""
    menuName: Optional[str] = None
    status: Optional[str] = None
    visible: Optional[str] = None


class MenuVO(BaseEntity):
    """菜单视图对象 - 匹配RuoYi sys_menu表结构"""
    menuId: int
    menuName: str
    parentName: Optional[str] = None
    parentId: int
    orderNum: int
    path: Optional[str] = None
    component: Optional[str] = None
    queryParam: Optional[str] = None  # 路由参数
    query: Optional[str] = None  # 兼容字段
    routeName: Optional[str] = None
    isFrame: str
    isCache: str
    menuType: str
    visible: str
    status: str
    perms: Optional[str] = None
    icon: Optional[str] = None
    remark: Optional[str] = None
    children: List["MenuVO"] = []


class MenuForm(BaseModel):
    """菜单表单 - 匹配RuoYi sys_menu表结构"""
    menuId: Optional[int] = None
    parentId: int = 0
    menuName: str
    orderNum: int = 0
    path: Optional[str] = ''
    component: Optional[str] = None
    queryParam: Optional[str] = None  # 路由参数
    query: Optional[str] = None  # 兼容字段
    routeName: Optional[str] = None
    isFrame: str = "1"  # 0是 1否
    isCache: str = "0"  # 0缓存 1不缓存
    menuType: str = "M"  # M目录 C菜单 F按钮
    visible: str = "0"  # 0显示 1隐藏
    status: str = "0"   # 0正常 1停用
    perms: Optional[str] = None
    icon: Optional[str] = '#'
    remark: Optional[str] = None


class MenuTreeOption(BaseModel):
    """菜单树选项"""
    id: int
    label: str
    children: List["MenuTreeOption"] = []


class RoleMenuTree(BaseModel):
    """角色菜单树"""
    checkedKeys: List[int]
    menus: List[MenuTreeOption]


# 路由相关
class RouterMeta(BaseModel):
    """路由元信息"""
    title: str
    icon: Optional[str] = None
    noCache: Optional[bool] = False
    link: Optional[str] = None


class RouterVO(BaseModel):
    """路由视图对象"""
    name: str
    path: str
    hidden: bool = False
    redirect: Optional[str] = None
    component: Optional[str] = None
    query: Optional[str] = None
    alwaysShow: Optional[bool] = None
    meta: RouterMeta
    children: List["RouterVO"] = []


# 更新前向引用
MenuVO.model_rebuild()
MenuTreeOption.model_rebuild()
RouterVO.model_rebuild()